<?php

/**
 * Description of DALSettledContract
 *
 * @author aethv
 */
class DALSettledContract extends DALBase{
    
    public function __construct() {
        parent::__construct();
        $this->_tbl = 'settled_contract';
        $this->_key = 'id';
    }
    
    public function getListSettled($orderBy='', $order = '', $p = 1, $perPage = 20){
        $data = array();
        $p_start = ($p == 1) ? 0 : ($p-1) * $perPage;
        
        $query = "SELECT c.`name` as 'country_name', l.`name` as 'league_name', sc.* FROM $this->_tbl sc";
        $query .= " LEFT JOIN league l ON l.id = sc.league_id";
        $query .= " LEFT JOIN country c ON l.countryId = c.id";
        if($orderBy){
            $query .= " ORDER BY sc.$orderBy ";
            
            $query .= ($order) ? $order : ' DESC ';
        }
        if($p != -1){
            $query .= ' LIMIT ' . $p_start . ", $perPage";
        }
//        echo $query;    
        $result = $this->executeQuery($query);
        if ($result) {
            //  Scan through the resource
            while ($row = mysql_fetch_object($result)) {
                //  put row object into the array
                $data[] = $row;
            }
        }else{
            echo 'Invalid query: ' . mysql_error();
        }
//        echo 'found: ' . count($data);
        return $data;
    }
    
    public function save($contract){
        
//        $contract = get_object_vars($obj);
//        print_r($contract);
        $id = $contract['id'];
        $tblID = $this->getByID($id);
        $query = "";
        
        $current_time = (int)$contract['current_time'];                
        if(!$tblID){ //update
            $query = "INSERT INTO " . $this->_tbl . " VALUES (";
                $query .= $contract['id'];
                $query .= ",'" . $contract['insert_timestamp'] . "'";
                $query .= ",'" . $contract['modify_timestamp'] . "'";
                $query .= "," . $contract['bet_amount'];
                $query .= ",'" . $contract['bet_slip'] . "'";
                $query .= "," . $contract['current_goal'];
                $query .= "," . $contract['dangerous'];
                $query .= "," . $contract['result_amount'];
                $query .= "," . $contract['running'];
                $query .= "," . $contract['settled_play_time'];
                $query .= "," . $contract['settled_total_goals'];
                $query .= "," . $contract['under_goal'];
                $query .= "," . $contract['won'];
                $query .= "," . $contract['won_amount'];
                $query .= "," . $contract['contract_id'];
                $query .= ",'" . $current_time . "'";
                $query .= ",'" . $contract['away_name'] . "'";
                $query .= ",'" . $contract['home_name'] . "'";
                $query .= ",'" . $contract['rate'] . "'";
                $query .= ",'" . $contract['league_id'] . "'";
            $query .= ");";
        }else{  //insert
            
            $away = $tblID['away_name'];
            $home = $tblID['home_name'];
            
            $away_name = (strlen($contract['away_name']) == 0 || trim($contract['away_name']) == '' ) ? $away : $contract['away_name'];
            $home_name = (strlen($contract['home_name']) == 0 || trim($contract['home_name']) == '' ) ? $home : $contract['home_name'];
            
            $query = "UPDATE " . $this->_tbl . " SET ";
                $query .= "`modify_timestamp`='"   . $contract['modify_timestamp'] . "'";
                $query .= ', `bet_amount`='          . $contract['bet_amount'];
                $query .= ", `bet_slip`='"           . $contract['bet_slip'] . "'";
                $query .= ', `current_goal`='        . $contract['current_goal'];
                $query .= ', `dangerous`='           . $contract['dangerous'];
                $query .= ', `result_amount`='       . $contract['result_amount'];
                $query .= ', `running`='             . $contract['running'];
                $query .= ', `settled_play_time`= '   . $contract['settled_play_time'];
                $query .= ', `settled_total_goals`=' . $contract['settled_total_goals'];
                $query .= ', `under_goal`='          . $contract['under_goal'];
                $query .= ', `won`='                 . $contract['won'];
                $query .= ", `rate`='"           . $contract['rate'] . "'";
                $query .= ", `home_name`='"           . $home_name . "'";
                $query .= ', `won_amount`='          . $contract['won_amount'];
                $query .= ', `contract_id`='         . $contract['contract_id'];
                $query .= ", `current_time`='"         . $current_time . "'";
                $query .= ", `away_name`='"           . $away_name . "'";
                $query .= ", `league_id`="           . $contract['league_id'] . "";
                $query .= " WHERE " . $this->_key . '=' . $contract['id'];
        }
//        echo $query;
        $result = $this->executeQuery($query);
        if($result){
            return mysql_affected_rows();
        }else{
            return -1;
        }
    }
    
}
